package com.warrantyhotel.android.ui;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;

import com.warrantyhotel.android.R;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.CountryTable;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.LanguageTable;
import com.warrantyhotel.android.model.Keys;
import com.warrantyhotel.android.util.ToastUtil;

public class SplashScreenActivity extends Activity {
	private static final long SPLASH_DISPLAY_LENGTH = 1500;
	private WarrantyApplication warrantyHotelApplication;
	private static SharedPreferences prefs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		prefs = PreferenceManager.getDefaultSharedPreferences(this);

		warrantyHotelApplication = (WarrantyApplication) getApplicationContext();
		if (!warrantyHotelApplication.isInternetPresent())
			ToastUtil.showNoNetwork(getApplicationContext());

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {

				Intent mainIntent = null;
				if (prefs.contains(Keys._password) && prefs.contains(Keys._username) && prefs.contains(Keys._remembeMe)) {
					mainIntent = new Intent(SplashScreenActivity.this, HomeActivity.class);
				} else {
					mainIntent = new Intent(SplashScreenActivity.this, LoginActivity.class);
				}
				startActivity(mainIntent);

				finish();
			}
		}, SPLASH_DISPLAY_LENGTH);

		ContentValues values = new ContentValues();
		values.put(CountryTable.NAME_HACK, RegistrationActivity.FAKE_ID);
		values.put(CountryTable.COUNTRY_ID, RegistrationActivity.FAKE_ID);
		values.put(CountryTable.NAME, getString(R.string.select_country));
		getContentResolver().insert(CountryTable.CONTENT_URI, values);

		values.clear();
		values.put(LanguageTable.LANGUAGE_ID, 0);
		values.put(LanguageTable.ENGLISH_NAME, getString(R.string.select_language));
		getContentResolver().insert(LanguageTable.CONTENT_URI, values);

	}

	// private ServiceConnection mConnection = new ServiceConnection() {
	// public void onServiceConnected(ComponentName className, IBinder service)
	// {
	// //DatabaseService mService = ((LocalBinder<DatabaseService>)
	// service).getService();
	//
	// // Get hold of your resource, db etc and pass it through to a object that
	// will use it
	// //manager = new MyQueryManager( mService.getMyDatabase());
	//
	// // do any extra setup that requires your Service
	// }
	//
	// public void onServiceDisconnected(ComponentName className) {
	// // As our service is in the same process, this should never be called
	// }
	// };

}
